﻿using SHDM.Web.DataAccess;
using SHDM.Web.Infrastructure;
using SHDM.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace SHDM.Web.Controllers
{
    [Authenticator]
    [RoutePrefix("api/users")]
    public class UsersController : ApiBaseController
    {
        [HttpPost]
        [Route("Query")]
        public OffsetQuery Query(List<int> lstDept, int offset, int limit)
        {
            string cmdText = string.Empty;
            if (lstDept != null && lstDept.Count > 0)
            {
                cmdText = $"SELECT * FROM SH_USERS WHERE Role <> 'Admin' AND DEPTID IN({string.Join(",", lstDept.ToArray())})";
            }
            else
            {
                cmdText = "SH_USERS WHERE Role <> 'Admin'";
            }

            return new OffsetQuery(cmdText,offset,limit);
        }

        [HttpGet]
        [Route("Del")]
        public Result Del(int id)
        {
            if(this.AccountInfo.Role != sh_users.Roles.Admin)
            {
                return new Result() { ErrorCode = 1, ErrorMessage = "没有权限" };
            }
            var db = new SHDMEntities();
            var user = db.sh_users.FirstOrDefault(e => e.ID == id);
            if(user == null)
            {
                return new Result() { ErrorCode=1, ErrorMessage="用户不存在" };
            }

            db.sh_users.Remove(user);
            db.SaveChanges();
            return new Result() { ErrorCode=0, ErrorMessage="操作成功" };
        }

        [HttpPost]
        [Route("Edit")]
        public Result Edit(sh_users model)
        {
            if (this.AccountInfo.Role != sh_users.Roles.Admin)
            {
                return new Result() { ErrorCode = 1, ErrorMessage = "没有权限" };
            }
            if (string.IsNullOrEmpty(model.Password))
            {
                return new Result() { ErrorCode = 1, ErrorMessage = "密码不能为空" };
            }
            if (string.IsNullOrEmpty(model.RealName))
            {
                return new Result() { ErrorCode = 1, ErrorMessage = "姓名不能为空" };
            }
            var db = new SHDMEntities();
            if (model.ID > 0)
            {
                var user = db.sh_users.FirstOrDefault(e => e.ID == model.ID);
                if (user == null)
                {
                    return new Result() { ErrorCode = 1, ErrorMessage = "用户不存在" };
                }
                user.RealName = model.RealName;
                user.State = model.State;
                user.Remark = model.Remark;
                if (string.Compare(user.Password, model.Password) != 0)
                {
                    user.Password = TextUtil.MD5Encrypt(model.Password);
                }
                db.SaveChanges();
            }
            else
            {
                if (string.IsNullOrEmpty(model.LoginName))
                {
                    return new Result() { ErrorCode = 1, ErrorMessage = "账号不能为空" };
                }
                if (db.sh_users.Any(e=>e.LoginName == model.LoginName))
                {
                    return new Result() { ErrorCode = 1, ErrorMessage = "账号已存在" };
                }
                if(model.DeptID != 0 && !db.sh_dept.Any(e=>e.ID == model.DeptID))
                {
                    return new Result() { ErrorCode = 1, ErrorMessage = "部门不存在" };
                }
                model.Role = sh_users.Roles.User;
                model.Password = TextUtil.MD5Encrypt(model.Password);
                model.CreateBy = this.AccountInfo.ID;
                model.CreateDate = DateTime.Now;
                db.sh_users.Add(model);
                db.SaveChanges();
            }

            return new Result() { ErrorCode = 0, ErrorMessage = "操作成功" };
        }
    }
}